package C031;

import C017.main;

/**
 * ClassName: Code06_CountOnesBinarySystem
 * Package: C031
 * Description:
 *
 * @Author BCXJ
 * @Create 2024/12/27 20:40
 * @Version 1.0
 * @Since 1.0
 */
public class Code06_CountOnesBinarySystem {
    public static void main(String[] args) {

        System.out.println(countOnesBinarySystem(0xff0f0f0f));
        System.out.println(countOnesBinarySystem(0b1100));
    }


    public static int countOnesBinarySystem(int n) {
        n = (n & 0x55555555) + ((n >>> 1) & 0x55555555);
        n = (n & 0x33333333) + ((n >>> 2) & 0x33333333);
        n = (n & 0x0f0f0f0f) + ((n >>> 4) & 0x0f0f0f0f);
        n = (n & 0x00ff00ff) + ((n >>> 8) & 0x00ff00ff);
        n = (n & 0x0000ffff) + ((n >>> 16) & 0x0000ffff);
        return n;
    }
}
